package io.renren.modules.substation.controller;

import java.util.*;

import io.renren.common.utils.Constant;
import io.renren.common.utils.DateUtils;
import io.renren.modules.substation.entity.SelectItemInfo;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.substation.entity.TBaseDictInfoEntity;
import io.renren.modules.substation.service.TBaseDictInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

/**
 * 系统字典信息表
 *
 * @author yzx
 * @email sunlightcs@gmail.com
 * @date 2020-06-13 15:45:23
 */
@RestController
@RequestMapping("substation/tbasedictinfo")
public class TBaseDictInfoController extends AbstractController {
    @Autowired
    private TBaseDictInfoService tBaseDictInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tbasedictinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tBaseDictInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{dictId}")
    @RequiresPermissions("substation:tbasedictinfo:info")
    public R info(@PathVariable("dictId") Long dictId){
		TBaseDictInfoEntity tBaseDictInfo = tBaseDictInfoService.getById(dictId);

        return R.ok().put("tBaseDictInfo", tBaseDictInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tbasedictinfo:save")
    public R save(@RequestBody TBaseDictInfoEntity tBaseDictInfo){
		tBaseDictInfoService.save(tBaseDictInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tbasedictinfo:update")
    public R update(@RequestBody TBaseDictInfoEntity tBaseDictInfo){
        tBaseDictInfo.setLastOpr(getUserId());
        tBaseDictInfo.setLastTime(new Date());
        tBaseDictInfoService.updateById(tBaseDictInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tbasedictinfo:delete")
    public R delete(@RequestBody Long[] dictIds){
		tBaseDictInfoService.removeByIds(Arrays.asList(dictIds));

        return R.ok();
    }

    /**
     * 获取字典信息列表
     */
    @RequestMapping("/getDictListByKey")
    @RequiresPermissions("substation:tbasedictinfo:getDictListByKey")
    public R getDictListByKey(@RequestParam String key){
        List<SelectItemInfo> retList = new ArrayList<SelectItemInfo>();
        SelectItemInfo temp0=new SelectItemInfo("--请选择--","");
        retList.add(temp0);

        List<TBaseDictInfoEntity> dic_List = tBaseDictInfoService.queryByDictKey(key);
        for(int i=0;i<dic_List.size();i++) {
            SelectItemInfo temp=new SelectItemInfo(dic_List.get(i).getDictDesc(),dic_List.get(i).getDictValue());
            retList.add(temp);
        }

        return R.ok().put("list", retList);
    }

    /**
     * 获取单字典信息-值
     */
    @RequestMapping("/getDictValueByKey")
    @RequiresPermissions("substation:tbasedictinfo:getDictValueByKey")
    public R getDictValueByKey(@RequestParam String key){
        String value = "";
        List<TBaseDictInfoEntity> list = tBaseDictInfoService.queryByDictKey(key);

        if (list != null && list.size() == 1){
            value = list.get(0).getDictValue();
        }

        return R.ok().put("data", value);
    }

    /**
     * 获取单字典信息-实体
     */
    @RequestMapping("/getDictInfoByKey")
    @RequiresPermissions("substation:tbasedictinfo:getDictInfoByKey")
    public R getDictInfoByKey(@RequestParam String key){
        TBaseDictInfoEntity info = new TBaseDictInfoEntity();
        List<TBaseDictInfoEntity> list = tBaseDictInfoService.queryByDictKey(key);

        if (list != null && list.size() == 1){
            info = list.get(0);
        }

        return R.ok().put("data", info);
    }

    /**
     * 获取稳定运行天数
     */
    @RequestMapping("/getSysWorkDays")
    @RequiresPermissions("substation:tbasedictinfo:getSysWorkDays")
    public R getSysWorkDays(){
//        logger.info("substation:tbasedictinfo:getSysWorkDays start...");
//        String value = "0";
//        List<TBaseDictInfoEntity> list = tBaseDictInfoService.queryByDictKey("SYS_START_WORK_DATE");
//
//        if (list != null && list.size() == 1){
//            String startTime = list.get(0).getDictValue();
//            if (startTime.length() != 8)
//                return R.ok().put("data", "0");
//            Date dateStart = DateUtils.stringToDate(startTime, "yyyyMMdd");
//            Long l = 0L;
//            long start = dateStart.getTime();
//            long end = new Date().getTime();
//            l = (end - start) / (1000 * 60 * 60 * 24);
//            value =  l.intValue() + "";
//        }
//        logger.info("substation:tbasedictinfo:getSysWorkDays end...");
        return R.ok().put("data", Constant.SYS_WORK_DAYS);
    }


}
